package com.blb.community_admin.controller;

import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysPost;
import com.blb.community_admin.entity.SysRole;
import com.blb.community_admin.entity.SysUser;
import com.blb.community_admin.service.IRoleService;
import com.blb.community_admin.service.ISysPostService;
import com.blb.community_admin.service.ISysUserService;
import com.blb.community_admin.util.ResponseResult;
import com.blb.community_admin.util.ResponseStatus;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api
@RestController
public class PersonalController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private ISysPostService sysPostService;

    public static final String DIR = "http://82.156.218.29/";

    @Autowired
    private FastFileStorageClient client;

    @Log(module = "菜单管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("根据用户名查询用户信息")
    @GetMapping("/personal/getUser/{username}")
    public ResponseResult<SysUser> getUser(@PathVariable String username){
        SysUser sysuser = sysUserService.getByName(username);
        return ResponseResult.ok(sysuser);
    }

    @Log(module = "菜单管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("根据userId查询权限信息")
    @GetMapping("/personal/getRoleByuserId/{userId}")
    public ResponseResult<SysRole> getRoleByuserId(@PathVariable long userId){
        SysRole roleByuserId = roleService.getRoleByuserId(userId);
        return ResponseResult.ok(roleByuserId);
    }

    @ApiOperation("根据userId查询post信息")
    @GetMapping("/personal/getPostByuserId/{userId}")
    public ResponseResult<SysPost> getPostByuserId(@PathVariable long userId){
        SysPost postByuserId = sysPostService.getPostByuserId(userId);
        return ResponseResult.ok(postByuserId);
    }

    @ApiOperation("修改用户信息")
    @PutMapping("/personal/updateUserByuserId")
    public ResponseResult<String> updateUserByuserId(@RequestBody SysUser sysUser){
        System.out.println(sysUser);
        sysUserService.updateById(sysUser);
        return ResponseResult.ok("updateform");
    }

    @ApiOperation("修改用户信息")
    @GetMapping("/personal/updatePasswordByuserId")
    public ResponseResult<String> updatePasswordByuserId(Long userId,String password,String oldpassword,String newpassword){
        System.out.println(userId);
        System.out.println(password);
        System.out.println(oldpassword);
        System.out.println(newpassword);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(oldpassword, password);
        if (matches) {
            String newpwd = bCryptPasswordEncoder.encode(newpassword);
            sysUserService.updatePwdById(userId,newpwd);
            return ResponseResult.ok("updatepassword");
        }
        else {
            return ResponseResult.error(ResponseStatus.BUSINESS_ERROR,"旧密码输入错误");
        }


    }

    @ApiOperation("确认旧密码是否正确")
    @GetMapping("/personal/checkPassword")
    public ResponseResult<String> checkPassword(String password,String oldpassword){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(oldpassword, password);
        if (matches){
            return ResponseResult.ok("ok");
        }else {
            return ResponseResult.error(ResponseStatus.BUSINESS_ERROR,"error");
        }

    }

    @ApiOperation("上传图片")
    @ResponseBody
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    synchronized public ResponseEntity<String> upload(MultipartFile file) throws IOException {
        //获得后缀名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传
        StorePath storePath = client.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        System.out.println("save:" + storePath.getFullPath());
        return ResponseEntity.ok(DIR + storePath.getFullPath());
    }

    @ApiOperation("保存图片地址")
    @GetMapping("/getImgUrl")
    public ResponseEntity<String> getImgUrl(long userId,String avatar){
        System.out.println(userId+avatar);
        SysUser byId = sysUserService.getById(userId);
        byId.setAvatar(avatar);
        sysUserService.updateUserById(byId);
        return ResponseEntity.ok("ok");
    }


}
